// 引入数据库
const conn = require('../data/gy_index');


// 后台项目页面显示 
module.exports.mange = (req, res) => {
    if (!req.session.isgaoyuan) {
        res.redirect('/gy_login');
        return;
    }
    res.render('zlg_mange')
}
// 项目页面渲染
module.exports.zlg_mange = (req, res) => {
    conn.query('select * from zlg_mange a,users b where a.users_id = b.gy_id and a.users_id=' + req.session.users.gy_id, (error, results) => {
        if (error) return console.log(error.message)
        if (results.lenght != 0) {
            res.json(results)
        }
    })
}
// 添加
module.exports.add = (req, res) => {
    if (!req.session.isgaoyuan) {
        res.redirect('/gy_login');
        return;
    }
    res.render('zlg_add')
}
// 项目页面
module.exports.project = (req, res) => {
    if (!req.session.isgaoyuan) {
        res.redirect('/gy_login');
        return;
    }
    conn.query('select * from zlg_mange a,users b where a.users_id = b.gy_id and a.id =' + req.query.id, (error, results) => {
        if (error) return console.log(error.message)
        res.render('zlg_project', results[0])
    })
}
// 添加数据
module.exports.insertUser = (req, res) => {
    let sql = 'insert into zlg_mange values (null,?,?,?,?,?,?)'
    let fromDate = [req.body.name, req.body.Code, req.body.started, req.body.ended, req.body.content, req.session.users.gy_id]
    conn.query(sql, fromDate, (error, results) => {
        if (error) return console.log(error.message)
        res.json({
            code: '1000',
            message: '添加项目成功'
        })
    })
}
//删除
module.exports.deleteUser = (req, res) => {
    conn.query('delete from zlg_mange where id=' + req.query.id, (error, results) => {
        if (error) return console.log(error.message)
        res.json({
            code: '1002',
            message: '删除成功'
        })
    })
}
//主页
module.exports.zlg_page = (req, res) => {
    if (!req.session.isgaoyuan) {
        res.redirect('/gy_login');
        return;
    }
    conn.query('select *,count(*) lkjh from zlg_mange a,users b where a.users_id = b.gy_id and gy_id=' + req.session.users.gy_id,(error, results) => {
        if (error) return console.log(error.message)
        res.render('zlg_page', results[0])
    })
}



